
import flyhttp from './httpInterceptors' 
const regUrl=(val)=>{
    let arr =val.split('/')
    return arr[arr.length-1]
};
export const postJson =(url,data={}) => {
    return new Promise((resolve,reject) => { 
        flyhttp.post(url,data,{headers:{token:wx.getStorageSync('h_token'),uid:wx.getStorageSync('u_id')}})
        .then(res=>{ 
            // wx.setStorageSync('h_token', '');
            // wx.setStorageSync('u_id',  ''); 
            if((res.data.code==404||res.data.code==403)&&regUrl(url)!='we-chat'){
                let url='./../login/main';
                wx.reLaunch({url});
                wx.showToast({icon:'none',title:res.data.data}); 
            }
            resolve(res); 
        }).catch(err=>{ 
            if(err.status==500||err.status==404){
                wx.showToast({icon:'none',title:'服务器错误！'})
                
            } 
            reject(err);
        })   
    })
}

 
export const fetch =(url,data={}) => { 
    return new Promise((resolve,reject)=>{ 
        flyhttp.get(url,data,{headers:{token:wx.getStorageSync('h_token'),uid:wx.getStorageSync('u_id')}})
        .then(res=>{
            if(res.data.code==404||res.data.code==403){
                let url='./../login/main';
                wx.reLaunch({url});
                wx.showToast({icon:'none',title:res.data.data});
            }
            resolve(res); 
        }).catch(err=>{
            if(err.status==500||err.status==404){
                wx.showToast({icon:'none',title:'服务器错误！'})
            } 
            reject(err);
        })   
    })
}
 